package com.ltxtiyu.video.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ltxtiyu.video.cacheDao.UserCacheDao;
import com.ltxtiyu.video.common.CodeTypeConstant;

import com.ltxtiyu.video.dao.ltx.AreaDao;
import com.ltxtiyu.video.dao.ltx.UserDao;
import com.ltxtiyu.video.entity.Area;
import com.ltxtiyu.video.entity.User;
import com.ltxtiyu.video.service.ms.UserService;
import com.ltxtiyu.video.vo.ms.AddressVO;
import com.ltxtiyu.video.vo.ms.UserVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private AreaDao areaDao;
    @Autowired
    private UserCacheDao userCacheDao;
    @Override
    public Map<String, Object> selectParamList(Map<String, Object> map) {
        int count = userDao.findByParamCount(map);
        List<UserVo> data= userDao.findByParamList(map);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","");
        result.put("count",count);
        result.put("data",data);
        return result;
    }

    @Override
    public void sealUser(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        user.setStatus(CodeTypeConstant.COMMON_TYPE_0);
        userCacheDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public void unSealUser(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        user.setStatus(CodeTypeConstant.COMMON_TYPE_1);
        userCacheDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<AddressVO> userAddress() {
        List<Area> province =  areaDao.findAllProvince();

        List<AddressVO> addressList=new ArrayList<>();

        for (Area area:province){
            AddressVO addressVO = new AddressVO();
            String name = area.getName();
            if (name.contains("省")){
                name = name.substring(0,name.length()-1);
            }
            addressVO.setAddress(name);
           int count = userDao.findNumberByProvinceId(area.getId());
           if (count > 0){
               addressVO.setNumber(count);
           }else {
               addressVO.setNumber(0);

           }
            addressList.add(addressVO);
        }
        return addressList;
    }
}
